//
//  MyBoardUIViewController.m
//  FanTest
//
//  Created by Cheng on 12/6/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyBoardUIViewController.h"
#import "AccountInfo.h"
#import "AccountMyBoard.h"
#import "AccountBoard.h"
#import "MediaListsMedia.h"
#import "MyBoardCell.h"
#import "VerticalCell.h"
#import "AddMyBoardCell.h"
#import "BK_Enviroment.h"
#import "RequestAPIFunction.h"
#import "Split.h"

#define SCREENHEIGHT 606
#define SCROLL_AXIS_Y 100
#define WIDTH_SPACE 20  
#define WIDTH_LEFTBORDER 20
#define SHARE_ARROW_AXIS_Y 135

#define LEFT_SPACE 10
#define BORDER_HEIGHT 10
#define SEND_EMAIL_ARROW_AXIS_Y 28
#define PHOTO_ARROW_AXIS_X 37
#define QUESTION_ARROW_AXIS_X 492

#define KEYBOARDHEIGHT 360
#define SCROLLVIEW_HEIGHT 606

#define MODIFYMYBOARD_API @"http://54.235.166.43/BeSpokeAPI/MyBoard/ModifyMyBoard.php"

@implementation MyBoardUIViewController
@synthesize Array_AccountInfo, BK_En_object, Vw_ShareMyBook, Vw_SendEmailSuccess, Vw_PhotoList;
@synthesize Vw_SendEmail, Vw_QuestionView, Vw_EditBookView;
@synthesize Vw_MyBoardCell,Vw_AddMyBoardCell,SVw_Main,IVw_AccountPhoto;
@synthesize delegate;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.IVw_AccountPhoto setImage:self.BK_En_object.yourPhoto];
}

- (void) viewWillAppear:(BOOL)animated {
    
    
    //Clean Cell View.
    [Vw_PhotoList removeFromSuperview];
    [Vw_EditBookView removeFromSuperview];
    [Vw_QuestionView removeFromSuperview];
    [Vw_SendEmail removeFromSuperview];
    [Vw_SendEmailSuccess removeFromSuperview];
    [Vw_ShareMyBook removeFromSuperview];
    SVw_Main.userInteractionEnabled = YES;
    
    int X_axis_MyBoardCell = 0;
    int Y_axis_MyBoardCell = 0;
    int X_axis_AddMyBoardCell = 0;
    int Y_axis_AddMyBoardCell = 0;

    SVw_Main = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SCROLL_AXIS_Y, self.view.bounds.size.width, SCREENHEIGHT)];
    SVw_Main.scrollEnabled = YES;
    [self.view addSubview:SVw_Main];
    int Media_Total = 0;
    
    for(AccountBoard *AB_Board in [[Array_AccountInfo objectAtIndex:0] MyBoard].BoardArray) {
        
        self.Vw_MyBoardCell = [[[NSBundle mainBundle] loadNibNamed:@"MyBoardCell" owner:self options:nil] objectAtIndex: 0];
        
        self.Vw_MyBoardCell.AB_CurrentBook = AB_Board;
        
        //Set Button Event.
        [self.Vw_MyBoardCell.Bt_Mybook addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        //Calculate AXIS.
        if(X_axis_MyBoardCell+WIDTH_LEFTBORDER+WIDTH_SPACE>self.view.bounds.size.width) {
            X_axis_MyBoardCell = 0;
            Y_axis_MyBoardCell = Y_axis_MyBoardCell+Vw_MyBoardCell.frame.size.height+BORDER_HEIGHT;
        }
        
        [Vw_MyBoardCell setFrame:CGRectMake(X_axis_MyBoardCell+WIDTH_LEFTBORDER, Y_axis_MyBoardCell, Vw_MyBoardCell.frame.size.width, Vw_MyBoardCell.frame.size.height)];
        X_axis_MyBoardCell = X_axis_MyBoardCell+Vw_MyBoardCell.frame.size.width+WIDTH_SPACE;
        X_axis_AddMyBoardCell = X_axis_MyBoardCell;
        
        if(X_axis_AddMyBoardCell+WIDTH_LEFTBORDER+WIDTH_SPACE>self.view.bounds.size.width) {
            Y_axis_AddMyBoardCell = Y_axis_MyBoardCell+Vw_MyBoardCell.frame.size.height+BORDER_HEIGHT;
            X_axis_AddMyBoardCell = 0;
        }
        
        
        [self.Vw_MyBoardCell updateBoardInfo];
        [self.SVw_Main addSubview:self.Vw_MyBoardCell];
        Media_Total = Media_Total + AB_Board.MediaArray.count;
    }
    
    self.Vw_AddMyBoardCell = [[[NSBundle mainBundle] loadNibNamed:@"AddMyBoardCell" owner:self options:nil] objectAtIndex: 0];
    
    [self.Vw_AddMyBoardCell setFrame:CGRectMake(X_axis_AddMyBoardCell+WIDTH_SPACE, Y_axis_AddMyBoardCell, Vw_AddMyBoardCell.frame.size.width, Vw_AddMyBoardCell.frame.size.height)];
    
    [self.SVw_Main addSubview:self.Vw_AddMyBoardCell];
    
    self.SVw_Main.contentSize = CGSizeMake(0,Y_axis_AddMyBoardCell+Vw_AddMyBoardCell.frame.size.height);
    
    [SVw_Main setCanCancelContentTouches:NO];
}

-(IBAction)AddBookPressed:(id)sender
{
    self.Vw_MyBoardCell = [[[NSBundle mainBundle] loadNibNamed:@"MyBoardCell" owner:self options:nil] objectAtIndex: 0];
    
    self.Vw_MyBoardCell.Bt_Share.hidden = YES;
    
    Vw_MyBoardCell.frame = CGRectMake(Vw_AddMyBoardCell.frame.origin.x,
                                      Vw_AddMyBoardCell.frame.origin.y,
                                      Vw_AddMyBoardCell.frame.size.width,
                                      Vw_AddMyBoardCell.frame.size.height);
    
    [self.Vw_MyBoardCell updateBoardInfo];
    
    //Animation
    [UIView animateWithDuration:0.5
                          delay:0
                        options: (UIViewAnimationOptions)UIViewAnimationCurveEaseOut
                     animations:^{
                         
                         int AXIS_X = 0;
                         int AXIS_Y = 0;
                         
                         if (Vw_MyBoardCell.frame.origin.x+Vw_MyBoardCell.frame.size.width+WIDTH_SPACE+Vw_AddMyBoardCell.frame.size.width>SVw_Main.frame.size.width) {
                             AXIS_X = WIDTH_LEFTBORDER;
                             AXIS_Y = Vw_MyBoardCell.frame.origin.y+Vw_MyBoardCell.frame.size.height+BORDER_HEIGHT;
                         }
                         else {
                             AXIS_X = Vw_MyBoardCell.frame.origin.x+Vw_MyBoardCell.frame.size.width+WIDTH_SPACE;
                             AXIS_Y = Vw_AddMyBoardCell.frame.origin.y;
                         }

                         
                         [Vw_AddMyBoardCell setFrame:CGRectMake(AXIS_X, AXIS_Y, Vw_AddMyBoardCell.frame.size.width, Vw_AddMyBoardCell.frame.size.height)];
                         
                         
                         
                         SVw_Main.contentSize = CGSizeMake(0, Vw_AddMyBoardCell.frame.origin.y+Vw_AddMyBoardCell.frame.size.height);}
                     completion:^(BOOL finished){ }];
    
    [Vw_MyBoardCell setAlpha:0.0];
    
    [SVw_Main addSubview:Vw_MyBoardCell];
    
    NSLog(@"Before::::::%@",SVw_Main.subviews);
    for (UIView *aaa in SVw_Main.subviews) {
        if (([aaa isKindOfClass:[UIImageView class]])||([aaa isKindOfClass:[AddMyBoardCell class]])) {
            [self.SVw_Main bringSubviewToFront:aaa];
        }
    }
    NSLog(@"After::::::%@",SVw_Main.subviews);
    
    [UIView animateWithDuration:1
                          delay:0.5
                        options: (UIViewAnimationOptions)UIViewAnimationCurveEaseOut
                     animations:^{[Vw_MyBoardCell setAlpha:1.0];}
                     completion:^(BOOL finished){
                         
                         
                     }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (Vw_ShareMyBook!=nil)
        [Vw_ShareMyBook removeFromSuperview];
    else if (Vw_EditBookView!=nil)
        [Vw_EditBookView removeFromSuperview];
}

- (IBAction)SharePressed:(id)sender {
    
    [self.view bringSubviewToFront:self.SVw_Main];
    
    if(self.Vw_ShareMyBook == nil) {
        self.Vw_ShareMyBook = [[[NSBundle mainBundle] loadNibNamed:@"ShareMyBookView" owner:self options:nil] objectAtIndex:0];
        self.Vw_ShareMyBook.delegate = self;
    }
    
    UIButton *Bt_Share = (UIButton *)sender;
    
    Vw_ShareMyBook.AB_CurrentBook = ((MyBoardCell *)Bt_Share.superview).AB_CurrentBook;
    
    //Judge Location of View.        
    self.Vw_ShareMyBook.frame = CGRectMake(Bt_Share.superview.frame.origin.x+Bt_Share.frame.origin.x+Bt_Share.frame.size.width,
                                           Bt_Share.superview.frame.origin.y+Bt_Share.center.y-SEND_EMAIL_ARROW_AXIS_Y,
                                           self.Vw_ShareMyBook.frame.size.width,
                                           self.Vw_ShareMyBook.frame.size.height);
    
    [self.Vw_ShareMyBook setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[self.Vw_ShareMyBook setAlpha:1.0];[self.SVw_Main addSubview:self.Vw_ShareMyBook];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)EditPressed:(id)sender {
    
    if (Vw_EditBookView == nil) {
        [[[NSBundle mainBundle] loadNibNamed:@"EditBookView" owner:self options:nil] objectAtIndex:0];
        Vw_EditBookView.delegate = self;
    }
    
    UIButton *Bt_Edit = (UIButton *)sender;
    Vw_EditBookView.BookCell = (MyBoardCell *)Bt_Edit.superview;
    
    int AXIS_X = 0;
    int AXIS_Y = 0;
    
    //Check view over screen width.
    if (Bt_Edit.superview.frame.origin.x+Bt_Edit.frame.origin.x+Bt_Edit.frame.size.width+Vw_EditBookView.frame.size.width>SVw_Main.frame.size.width) {
        AXIS_X = Bt_Edit.superview.frame.origin.x+Bt_Edit.frame.origin.x-Vw_EditBookView.frame.size.width;
        Vw_EditBookView.IVw_Window.image = [UIImage imageNamed:@"Edit_Book_Popup_RightArrow.png"];
    }
    else {
        AXIS_X = Bt_Edit.superview.frame.origin.x+Bt_Edit.frame.origin.x+Bt_Edit.frame.size.width;
        Vw_EditBookView.IVw_Window.image = [UIImage imageNamed:@"Edit_Book_Popup.png"];
    }
    
    //Check view over screen height.
    if (Bt_Edit.superview.frame.origin.y+Bt_Edit.center.y-Vw_EditBookView.frame.size.height/2+Vw_EditBookView.frame.size.height>SVw_Main.contentSize.height)
        AXIS_Y = SVw_Main.contentSize.height-Vw_EditBookView.frame.size.height;
    else
        AXIS_Y = Bt_Edit.superview.frame.origin.y+Bt_Edit.center.y-Vw_EditBookView.frame.size.height/2;
    
    
    
    Vw_EditBookView.frame = CGRectMake(AXIS_X,
                                       AXIS_Y,
                                       Vw_EditBookView.frame.size.width,
                                       Vw_EditBookView.frame.size.height);
    
    [Vw_EditBookView setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditBookView setAlpha:1.0];[self.SVw_Main addSubview:Vw_EditBookView];}
                     completion:^(BOOL finished){ }];
}

- (void)buttonPressed:(id)sender {
    
    if (![((MyBoardCell *)((UIButton *)sender).superview).Lab_Title.text isEqualToString:@""]) {
        
        [self performSegueWithIdentifier:@"DetailMyBoard_Segue" sender:sender];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Bottom Feature

-(IBAction)OpenStream:(id)sender {
    [self.delegate MyBoardUIViewControllerDidStream:self];
}

-(IBAction)OpenAccount:(id)sender {
    [self.delegate MyBoardUIViewControllerDidAccount:self];
}

-(IBAction)OpenFeedSet:(id)sender {
    [self.delegate MyBoardUIViewControllerDidFeedSet:self];
}

-(IBAction)MyPhotoPressed:(id)sender {
    
    SVw_Main.userInteractionEnabled = NO;
    if (Vw_PhotoList == nil) {
        
        UIButton *Bt_Photo = (UIButton *)sender;
        
        Vw_PhotoList = [[[NSBundle mainBundle] loadNibNamed:@"SelectMyPhoto" owner:self options:nil] objectAtIndex:0];
        Vw_PhotoList.delegate = self;
        Vw_PhotoList.BK_En_object = self.BK_En_object;
        [Vw_PhotoList GetController:self UserInfo:Array_AccountInfo];
        Vw_PhotoList.frame = CGRectMake(Bt_Photo.frame.origin.x+Bt_Photo.frame.size.width/2-PHOTO_ARROW_AXIS_X,
                                        Bt_Photo.superview.frame.origin.y-Vw_PhotoList.frame.size.height,
                                        Vw_PhotoList.frame.size.width,
                                        Vw_PhotoList.frame.size.height);
    }
    
    [Vw_PhotoList setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_PhotoList setAlpha:1.0];[self.view addSubview:Vw_PhotoList];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)QuestionPressed:(id)sender {
    
    if (Vw_QuestionView == nil) {
        
        UIButton *Bt_Question = (UIButton *)sender;
        
        Vw_QuestionView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionView" owner:self options:nil] objectAtIndex:0];
        Vw_QuestionView.frame = CGRectMake(Bt_Question.center.x-QUESTION_ARROW_AXIS_X,
                                           Bt_Question.superview.frame.origin.y-Vw_QuestionView.frame.size.height,
                                           Vw_QuestionView.frame.size.width,
                                           Vw_QuestionView.frame.size.height);
        
        Vw_QuestionView.delegate = self;
        [Vw_QuestionView Initialize];
    }
    
    //Check Tutorial Content.
    if (Vw_SendEmailSuccess==nil)
        Vw_QuestionView.SharedCheck = FALSE;
    else
        Vw_QuestionView.SharedCheck = TRUE;
    
    
    [Vw_QuestionView setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_QuestionView setAlpha:1.0];[self.view addSubview:Vw_QuestionView];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

#pragma mark - ShareMyBookViewDelegate

- (void)uiViewTouched_ShareMyBookView:(BOOL)wasInSide {
    
    if(!wasInSide) {
        [self.Vw_ShareMyBook removeFromSuperview];
    }
}

- (void)OpenEmailView {
    
    [self.Vw_ShareMyBook removeFromSuperview];
    
    if (self.Vw_SendEmail==nil) {
        self.Vw_SendEmail = [[[NSBundle mainBundle] loadNibNamed:@"SendEmailView" owner:self options:nil] objectAtIndex:0];
        self.Vw_SendEmail.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
        self.Vw_SendEmail.BK_En_object = self.BK_En_object;
        self.Vw_SendEmail.delegate = self;
    }
    self.Vw_SendEmail.AB_CurrentBook = self.Vw_ShareMyBook.AB_CurrentBook;
    [self.Vw_SendEmail InitTextView];
    self.SVw_Main.userInteractionEnabled = NO;
    [self.Vw_SendEmail setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[self.Vw_SendEmail setAlpha:1.0];[self.view addSubview:self.Vw_SendEmail];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - SendEmailViewDelegate

- (void)DidCloseView {
    [self.Vw_SendEmail removeFromSuperview];
    self.SVw_Main.userInteractionEnabled = YES;
}

- (void)SendEmailSuccessed:(NSMutableArray *)EmailArray {
    
    [self.Vw_SendEmail removeFromSuperview];
    
    if (self.Vw_SendEmailSuccess==nil) {
        self.Vw_SendEmailSuccess = [[[NSBundle mainBundle] loadNibNamed:@"SendEmailSuccessView" owner:self options:nil] objectAtIndex:0];
        self.Vw_SendEmailSuccess.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
        self.Vw_SendEmailSuccess.delegate = self;
    }
    [self.Vw_SendEmailSuccess InitEmailArray:EmailArray];
    [self.Vw_SendEmailSuccess setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[self.Vw_SendEmailSuccess setAlpha:1.0];[self.view addSubview:self.Vw_SendEmailSuccess];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - SendEmailSuccessViewDelegate

- (void)DidCloseView_SendEmailSuccessView {
    
    [self.Vw_SendEmailSuccess removeFromSuperview];
    self.SVw_Main.userInteractionEnabled = YES;
}


#pragma mark - EditBookViewDelegate

- (void)uiViewTouched_EditBookView:(BOOL)wasInSide {
    
    if(!wasInSide)
        [Vw_EditBookView removeFromSuperview];
}

- (void)uiViewRenameSelected {
    
    [Vw_EditBookView removeFromSuperview];
    
    Vw_EditBookView.BookCell.TF_Edit.delegate = self;
    [Vw_EditBookView.BookCell.TF_Edit becomeFirstResponder];
}

- (void)uiViewDeleteSelected {
    
    [Vw_EditBookView removeFromSuperview];
    
    UIAlertView *AlertView = [[UIAlertView alloc] initWithTitle:@"Are you sure?"
                                                        message:@"Are you sure you want to delete this book?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Ok",nil];
    [AlertView show];
    return;
}

-(void)Delete_Calculate_AXIS {
    
    int index = [SVw_Main.subviews indexOfObject:Vw_EditBookView.BookCell];
    
    //Animation.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.7];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    //View of animation.
    for (int i=SVw_Main.subviews.count-1; index<i; i--) {
        
        if ([[SVw_Main.subviews objectAtIndex:i] isKindOfClass:[MyBoardCell class]])
            ((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame = CGRectMake(((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i-1]).frame.origin.x,
                                                                                    ((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i-1]).frame.origin.y,
                                                                                    ((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.size.width,
                                                                                    ((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.size.height);
        else if ([[SVw_Main.subviews objectAtIndex:i] isKindOfClass:[AddMyBoardCell class]]) {
            ((AddMyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame = CGRectMake(((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i-1]).frame.origin.x,
                                                                                       ((MyBoardCell *)[SVw_Main.subviews objectAtIndex:i-1]).frame.origin.y,
                                                                                       ((AddMyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.size.width,
                                                                                       ((AddMyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.size.height);
            SVw_Main.contentSize = CGSizeMake(0, ((AddMyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.origin.y+((AddMyBoardCell *)[SVw_Main.subviews objectAtIndex:i]).frame.size.height);
        }
    }
    
    [((MyBoardCell *)[SVw_Main.subviews objectAtIndex:index]) removeFromSuperview];
    [UIView commitAnimations];
    [((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray removeObject:Vw_EditBookView.BookCell.AB_CurrentBook];
}

#pragma mark - SelectMyPhotoDelegate

-(void) uiViewTouched_SelectMyPhoto:(BOOL)wasInSide {
    if(!wasInSide) {
        [Vw_PhotoList removeFromSuperview];
        self.SVw_Main.userInteractionEnabled = YES;
    }
}

- (void)PhotoSaveSuccess:(NSString *)BoardName {
    [SVw_Main removeFromSuperview];
    SVw_Main = nil;
    [Vw_PhotoList removeFromSuperview];
    self.SVw_Main.userInteractionEnabled = YES;
    
    NSString *NS_Message = [NSString stringWithFormat:@"Great! This photo has been clipped to your \"%@\" Books",BoardName];
    
    UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"Success" message:NS_Message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    
    [AlertView show];
}

#pragma mark - QuestionViewDelegate

- (void)uiViewTouched_Question:(BOOL)wasInSide {
    
    if(!wasInSide)
        [Vw_QuestionView removeFromSuperview];
}

#pragma mark -
#pragma mark Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if([[segue identifier] isEqualToString:@"DetailMyBoard_Segue"]) {
        
        DetailMyBoardUIViewController *VC_detailmyboard = segue.destinationViewController;
        VC_detailmyboard.AB_DetailMyboard = ((MyBoardCell *)((UIButton *)sender).superview).AB_CurrentBook;
        VC_detailmyboard.Array_AccountInfo = self.Array_AccountInfo;
        VC_detailmyboard.BK_En_object = self.BK_En_object;
        VC_detailmyboard.delegate = self;
    }
}

#pragma mark -
#pragma mark MyBoardViewControllerDelegate

-(void)DetailMyBoardUIViewControllerDidClose:(DetailMyBoardUIViewController *)controller {
    
    [SVw_Main removeFromSuperview];
    SVw_Main = nil;
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)DetailMyBoardUIViewControllerDidStream:(DetailMyBoardUIViewController *)controller {
    
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [SVw_Main removeFromSuperview];
    SVw_Main = nil;
    [self.delegate MyBoardUIViewControllerDidStream:controller];
}

-(void)DetailMyBoardUIViewControllerDidAccount:(DetailMyBoardUIViewController *)controller {
    [SVw_Main removeFromSuperview];
    SVw_Main = nil;
    [self.delegate MyBoardUIViewControllerDidAccount:controller];
}

-(void)DetailMyBoardUIViewControllerDidFeedSet:(DetailMyBoardUIViewController *)controller {
    
    [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    [SVw_Main removeFromSuperview];
    SVw_Main = nil;
    [self.delegate MyBoardUIViewControllerDidFeedSet:controller];
}


#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    CGRect frameRect = ((MyBoardCell *)textField.superview).TF_Edit.frame;
    frameRect.size.height = 20;
    textField.frame = frameRect;
    
    ((MyBoardCell *)textField.superview).Lab_Title.hidden = YES;
    textField.hidden = NO;
    [textField setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:13]];
    textField.autocapitalizationType = NO;
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    [textField setText:((MyBoardCell *)textField.superview).Lab_Title.text];
    [textField setReturnKeyType:UIReturnKeyDone];
    [textField setBackgroundColor:[UIColor whiteColor]];
    [(MyBoardCell *)textField.superview addSubview:textField];
    
    CGPoint scrollPoint = CGPointMake(0.0,
                                      //This Height is between bottom of scrollview and AddAnotherUser button
                                      ((MyBoardCell *)textField.superview).frame.origin.y);
    [(UIScrollView *)((MyBoardCell *)textField.superview).superview setContentOffset:scrollPoint animated:NO];

    
    ((MyBoardCell *)textField.superview).superview.frame =CGRectMake(((MyBoardCell *)textField.superview).superview.frame.origin.x,
                                                         ((MyBoardCell *)textField.superview).superview.frame.origin.y,
                                                         ((MyBoardCell *)textField.superview).superview.frame.size.width,
                                                         self.view.bounds.size.height-((MyBoardCell *)textField.superview).superview.frame.origin.y-KEYBOARDHEIGHT);
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    ((MyBoardCell *)textField.superview).Lab_Title.hidden = NO;
    
    ((MyBoardCell *)textField.superview).superview.frame =CGRectMake(((MyBoardCell *)textField.superview).superview.frame.origin.x,
                                                                     ((MyBoardCell *)textField.superview).superview.frame.origin.y,
                                                                     ((MyBoardCell *)textField.superview).superview.frame.size.width,
                                                                    SCROLLVIEW_HEIGHT);
    [textField removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (![textField.text isEqualToString:@""]) {
        
        NSString *NS_Action = @"Modify";
        
        if (((MyBoardCell *)textField.superview).AB_CurrentBook.BoardID==0)
            NS_Action = @"Create";
        
        NSData *responseData =
        [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<AccountInfo UserName=\"%@\" Level=\"\" Name=\"%@\" Action=\"%@\"><Board ID=\"%d\" Name=\"%@\" /></AccountInfo>",
                                             self.BK_En_object.userName,
                                             self.BK_En_object.fullName,
                                             NS_Action,
                                             ((MyBoardCell *)textField.superview).AB_CurrentBook.BoardID,
                                             [textField.text SymbolParseString]]
                                  urlString:MODIFYMYBOARD_API];
        
        //No Network Connection or time out.
        if (responseData == nil)
            return YES;
        
        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData options:0 error:&error];
        
        //Check to upload is success.
        if ([[doc nodesForXPath:@"//Success" error:&error] count]!=0) {
            
            
            NSArray *Array_Success = [doc nodesForXPath:@"//Success" error:nil];
            
            int BoardID = [[[[Array_Success objectAtIndex:0] attributeForName:@"BoardID"] stringValue] intValue];
            
            //Check This book is Created or Modified.
            if (BoardID!=((MyBoardCell *)textField.superview).AB_CurrentBook.BoardID) {
                //Set Button Event.
                [((MyBoardCell *)textField.superview).Bt_Mybook addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                AccountBoard *AB_NilBoard = [[AccountBoard alloc] BoardID:BoardID name:textField.text ShareGroup:@""];
                [[[self.Array_AccountInfo objectAtIndex:0] MyBoard].BoardArray addObject:AB_NilBoard];
                ((MyBoardCell *)textField.superview).AB_CurrentBook = AB_NilBoard;
            }
            else {
                int Book_Index = [[[self.Array_AccountInfo objectAtIndex:0] MyBoard].BoardArray indexOfObject:((MyBoardCell *)textField.superview).AB_CurrentBook];
                ((AccountBoard *)[[[self.Array_AccountInfo objectAtIndex:0] MyBoard].BoardArray objectAtIndex:Book_Index]).name = textField.text;
            }
        }
    }
    
    ((MyBoardCell *)textField.superview).superview.frame =CGRectMake(((MyBoardCell *)textField.superview).superview.frame.origin.x,
                                                                     ((MyBoardCell *)textField.superview).superview.frame.origin.y,
                                                                     ((MyBoardCell *)textField.superview).superview.frame.size.width,
                                                         SCROLLVIEW_HEIGHT);
    [((MyBoardCell *)textField.superview).Lab_Title setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:13]];
    ((MyBoardCell *)textField.superview).Lab_Title.text = textField.text;
    ((MyBoardCell *)textField.superview).Bt_Share.hidden = NO;
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {

    if (buttonIndex==1) {
        //Check Book not is in database.
        if (Vw_EditBookView.BookCell.AB_CurrentBook==nil) {
            [self Delete_Calculate_AXIS];
            return;
        }
        
        NSData *responseData =
        [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<AccountInfo UserName=\"%@\" Level=\"\" Name=\"%@\" Action=\"Delete\"><Board ID=\"%d\" Name=\"\" /></AccountInfo>",
                                             self.BK_En_object.userName,
                                             self.BK_En_object.fullName,
                                             Vw_EditBookView.BookCell.AB_CurrentBook.BoardID]
                                  urlString:MODIFYMYBOARD_API];
        
        //No Network Connection or time out.
        if (responseData == nil)
            return;
        
        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:responseData options:0 error:&error];
        
        //Check to upload is success.
        if ([[doc nodesForXPath:@"//Success" error:&error] count]!=0)
            [self Delete_Calculate_AXIS];
    }
}

@end
